using System;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

using ipi.MCC.Controls;
using ipi.MCC.Global;

namespace ipi.MCC.Pages
{
    public class SitesTreeControl : System.Web.UI.UserControl
	{        
        protected delegate void AddChildToTree(Object oParent, TreeNodeCollection CurrentNodes);

        protected AddChildToTree FillWebApplication;
        protected AddChildToTree FillSite;
        protected AddChildToTree FillWeb;
        
        protected CustomValidator validGlobal;
        protected TreeView trvSitesToSearch;

        protected Label lblSitesToSearch;

        protected System.Collections.Generic.List<string> lCheckedItems;
        public System.Collections.Generic.List<string> CheckedItems
        {
            set
            {
                lCheckedItems = value;
            }

            get
            {
                lCheckedItems = new System.Collections.Generic.List<string>();
                foreach (TreeNode tnCurrentChecked in trvSitesToSearch.CheckedNodes)
                {
                    lCheckedItems.Add(tnCurrentChecked.Value);
                }

                return lCheckedItems;
            }
        }
        
        private TreeNodeSelectAction _SelectAction;
        public TreeNodeSelectAction SelectAction
        {
            set
            {
                _SelectAction = value;
            }

            get
            {
                return _SelectAction;
            }
        }

        private TreeNodeTypes _ShowCheckBoxes;
        public TreeNodeTypes ShowCheckBoxes
        {
            set
            {
                _ShowCheckBoxes = value;
            }

            get
            {
                return _ShowCheckBoxes;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    lblSitesToSearch.Text = Resources.Resources.MCCTreeSitesControl_lblSitesToSearch;
                    
                    FillTree();                    
                }
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }
        }

        public virtual void FillTree()
        {
            FillWebApplication = new AddChildToTree(FillTreeWebApplication);
            FillSite = new AddChildToTree(FillTreeSite);
            
            trvSitesToSearch.Nodes.Clear();
            trvSitesToSearch.ExpandDepth = 1;
            trvSitesToSearch.EnableClientScript = true;
            trvSitesToSearch.PopulateNodesFromClient = false;
            trvSitesToSearch.ShowCheckBoxes = _ShowCheckBoxes;

            if (FillWebApplication != null)
            {
                SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate
                {
                    FillWebApplication(SPWebService.ContentService.WebApplications, trvSitesToSearch.Nodes);
                }));
            }

            CheckItems();
        }
        
        protected virtual void FillTreeWebApplication(Object oParent, TreeNodeCollection CurrentNodes)
        {
            try
            {
                if (oParent is SPWebApplicationCollection)
                {
                    foreach (SPWebApplication spwaCurrent in (SPWebApplicationCollection)oParent)
                    {
                        if (AddItem(spwaCurrent))
                        {
                            TreeNode tnCurrent = new TreeNode();
                            tnCurrent.SelectAction = _SelectAction;
                            tnCurrent.Text = spwaCurrent.Name;
                            tnCurrent.Value = spwaCurrent.Id.ToString();

                            if (FillSite != null)
                            {
                                FillSite(spwaCurrent.Sites, tnCurrent.ChildNodes);
                            }
                            else
                            {
                                tnCurrent.SelectAction = TreeNodeSelectAction.None;
                            }

                            CurrentNodes.Add(tnCurrent);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }
        }

        protected virtual void FillTreeSite(Object oParent, TreeNodeCollection CurrentNodes)
        {
            try
            {
                if (oParent is SPSiteCollection)
                {
                    foreach (SPSite spsCurrent in (SPSiteCollection)oParent)
                    {
                        if(AddItem(spsCurrent))
                        {
                            TreeNode tnCurrent = new TreeNode();
                            tnCurrent.SelectAction = _SelectAction;
                            tnCurrent.Text = spsCurrent.Url;
                            tnCurrent.Value = spsCurrent.ID.ToString();

                            if (FillWeb != null)
                            {
                                FillWeb(spsCurrent.RootWeb, tnCurrent.ChildNodes);
                            }
                            else
                            {
                                tnCurrent.SelectAction = TreeNodeSelectAction.None;
                            }

                            CurrentNodes.Add(tnCurrent);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }
        }

        private void CheckItems()
        {
            if (lCheckedItems != null)
            {
                CheckNodes(trvSitesToSearch.Nodes);
            }
        }

        private void CheckNodes(TreeNodeCollection tncToCheck)
        {
            foreach (TreeNode tnToCheck in tncToCheck)
            {
                CheckNode(tnToCheck);
                CheckNodes(tnToCheck.ChildNodes);
            }
        }

        protected virtual void CheckNode(TreeNode tnCurrent)
        {

        }

        protected virtual bool AddItem(object oItem)
        {
            return true;
        }
	}
}